<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板状态" prop="templateType">
        <el-select v-model="queryParams.templateType" placeholder="请选择" clearable style="width: 240px">
          <el-option
            v-for="dict in dict.type.sms_templat_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 240px">
          <el-option
            v-for="dict in dict.type.sms_config_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:integrate:smsConfig:add']"
        >新建分组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:integrate:smsConfig:delete']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="smsConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="模板类型" align="center" prop="templateType" width="80" >
        <template slot-scope="scope"><!--模板类型：0-断电告警；1-缺相告警；2-通电恢复；3-设备通信异常告警")-->
          <dict-tag :options="dict.type.sms_templat_type" :value="scope.row.templateType"/>
        </template>
      </el-table-column>
      <el-table-column label="签名" align="center" prop="signName" width="80" />
      <el-table-column label="模板CODE" align="center" prop="templateCode" width="180" />
      <el-table-column label="密钥ID" align="center" prop="accessKeyId" width="230" />
      <el-table-column label="密钥值" align="center" prop="accessKeySecret" width="280" />
      <el-table-column label="模板状态" align="center" prop="status" width="80" >
        <template slot-scope="scope">
          <!--状态：0-停用，1-启用-->
          <el-tag type="danger" v-if="scope.row.status=='0'">停用</el-tag>
          <el-tag type="success" v-if="scope.row.status=='1'">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center" class-name="small-padding fixed-width"  fixed="right" width="280" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:integrate:smsConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-unlock"
            v-if="scope.row.status=='0'"
            @click="enableHandle(scope.row)"
            v-hasPermi="['business:integrate:smsConfig:edit']"
          >启用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-lock"
            v-if="scope.row.status=='1'"
            @click="disableHandle(scope.row)"
            v-hasPermi="['business:integrate:smsConfig:edit']"
          >禁用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: #f56c6c"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:integrate:smsConfig:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-drawer size="40%"
               :title="title"
               :visible.sync="open"
               :wrapperClosable="false"
               direction="rtl"
               custom-class="form-drawer"
               ref="drawer"
    >
      <el-form ref="form" :model="form" :rules="rules"  class="drawer-box" style="padding:10px 30px;overflow-x: hidden;overflow-y: auto;height: calc(100% - 56px);border-bottom:1px solid #eee;">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="签名" prop="signName">
              <el-input v-model="form.signName" :disabled="disabled" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模板Code" prop="templateCode">
              <el-input v-model="form.templateCode" :disabled="disabled" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="模板类型" prop="templateType">
              <el-radio-group v-model="form.templateType">
                <el-radio
                  v-for="dict in dict.type.sms_templat_type"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="密钥ID" prop="accessKeyId">
              <el-input v-model="form.accessKeyId" :disabled="disabled" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="密钥值" prop="accessKeySecret">
              <el-input v-model="form.accessKeySecret" :disabled="disabled" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="模板状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sms_config_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input :disabled="disabled" v-model="form.remark" type="textarea" :rows="4"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="drawer-footer"  style="position: absolute;bottom:0px;display:flex;left:50%;padding:10px 0;transform:translate(-50%,0);">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="danger" plain @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  import { listSmsConfig, getSmsConfig, addSmsConfig, editSmsConfig, delSmsConfig,enable,disable} from "@/api/business/integrate/smsConfig"
  export default {
    name: "SmsConfig",
    dicts: ['sms_config_status','sms_templat_type'],
    data() {
      return {
        addOrEdit:false,
        disabled:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 短信配置列表数据
        smsConfigList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          templateType: undefined,
          status: undefined,
        },
        // 表单参数
        form: {
          accessKeyId: undefined,
          accessKeySecret: undefined,
          signName: undefined,
          templateCode: undefined,
          templateType:"1",
          status:"1",
        },
        // 表单校验
        rules: {
          accessKeyId: [
            { required: true, message: "秘钥ID不能为空", trigger: "blur" }
          ],
          accessKeySecret: [
            { required: true, message: "秘钥值不能为空", trigger: "blur" }
          ],
          signName: [
            { required: true, message: "签名不能为空", trigger: "blur" }
          ],
          templateCode: [
            { required: true, message: "模版Code不能为空", trigger: "blur" }
          ],
        },
        multipleAlarmPhone: true,
        alarmPhoneSelectList:[],//手机号设置
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询短信配置列表 */
      getList() {
        this.loading = true
        listSmsConfig(this.queryParams).then(response => {
          this.smsConfigList = response.rows
          this.total = response.total
          this.loading = false
        })

      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          accessKeyId: undefined,
          accessKeySecret: undefined,
          signName: undefined,
          templateCode: undefined,
          templateType:"1",
          status:"1",
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.addOrEdit=false
        this.title = "新增"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.addOrEdit=true
        const groupId = row.id || this.ids
        getSmsConfig(groupId).then(response => {
          this.form = response.data
          this.open = true
          this.title = "编辑"
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              editSmsConfig(this.form).then(response => {
                this.$modal.msgSuccess("编辑成功")
                this.open = false
                this.getList()
              })
            } else {
              addSmsConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const groupIds = row.id || this.ids
        this.$modal.confirm('是否确认删除该短信配置吗？').then(function() {
          return delSmsConfig(groupIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      /** 启用按钮操作 */
      enableHandle(row) {
        const groupIds = row.id || this.ids
        this.$modal.confirm('是否启用该短信配置？').then(function() {
          return enable(groupIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("启用成功")
        }).catch(() => {})
      },
      /** 禁用按钮操作 */
      disableHandle(row) {
        const groupIds = row.id || this.ids
        this.$modal.confirm('是否禁用该短信配置？').then(function() {
          return disable(groupIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("禁用成功")
        }).catch(() => {})
      },
    }
  }
</script>
<style scoped lang="scss">
  .title{
    font-size: 16px;
    font-weight: bold;
    display: flex;
    align-items: center;
    color: #333;
    margin: 0;
    padding-bottom:10px;
    &::before{
      content: '';
      width: 5px;
      height: 16px;
      background: #1890FF;
      border-radius: 3px;
      display: inline-block;
      margin-right: 10px;
    }
  }
</style>
